package com.hou.camera;

import android.app.Activity;
import android.content.Intent;

import androidx.fragment.app.Fragment;

/**
 * @Author: HOU
 * @Date: 2022/9/6 15:18
 * @Desc 相机
 */
public class CardCamera {

    public final static int TYPE_IDCARD_FRONT = 1;//身份证正面
    public final static int TYPE_IDCARD_BACK = 2;//身份证反面
    public final static int TYPE_IDCARD_HAND = 3;//身份证手持
    public final static int TYPE_OTHER = 4;//其他证件
    public final static int TYPE_NORMAL = 5;//无证件类型
    public final static int RESULT_CODE = 0X11;//结果码
    public final static int PERMISSION_CODE_FIRST = 0x12;//权限请求码
    public final static String TAKE_TYPE = "take_type";//拍摄类型标记
    public final static String CAMERA_TYPE = "camera_id";//摄像头类型
    public final static String IMAGE_PATH = "image_path";//图片路径标记
    public final static String CROP_MODE = "CROP_MODE";//裁剪属性，自动/手动
    private int idCardDirection, cameraId;
    private boolean cropMode;
    private Activity mContext;
    private Fragment mFragment;

    public static CardCamera.Builder init() {
        return new CardCamera.Builder();
    }

    private CardCamera(Builder builder) {
        this.idCardDirection = builder.idCardDirection;
        this.cameraId = builder.cameraId;
        this.cropMode = builder.cropMode;
        this.mContext = builder.mContext;
        this.mFragment = builder.mFragment;
    }

    public static class Builder {
        private int idCardDirection, cameraId;
        private boolean cropMode;
        private Activity mContext;
        private Fragment mFragment;

        /**
         * 在activity中使用
         *
         * @param context
         * @return
         */
        public Builder setActivity(Activity context) {
            this.mContext = context;
            return this;
        }

        /**
         * 在fragment中使用
         *
         * @param context
         * @return
         */
        public Builder setFragment(Fragment context) {
            this.mFragment = context;
            return this;
        }

        /**
         * 设置卡证类型
         *
         * @param idCardDirection
         * @return TYPE_IDCARD_FRONT
         * TYPE_IDCARD_BACK
         * TYPE_IDCARD_HAND
         * TYPE_OTHER
         * TYPE_NORMAL
         */
        public Builder setIdCardDirection(int idCardDirection) {
            this.idCardDirection = idCardDirection;
            return this;
        }

        /**
         * 设置摄像头
         *
         * @param cameraId
         * @return CAMERA_FRONT
         * CAMERA_BACK
         */
        public Builder setIdCameraId(int cameraId) {
            this.cameraId = cameraId;
            return this;
        }

        /**
         * 设置是否自动裁剪
         *
         * @param cropMode
         * @return ture 自动裁剪
         * false 手动裁剪
         */
        public Builder setCropMode(boolean cropMode) {
            this.cropMode = cropMode;
            return this;
        }

        public CardCamera build() {
            CardCamera camera = new CardCamera(this);
            camera.openCamera();
            return camera;
        }
    }

    /**
     * 打开相机
     */
    public void openCamera() {
        if (mContext != null) {
            Intent intent = new Intent(mContext, CameraActivity.class);
            intent.putExtra(TAKE_TYPE, idCardDirection);
            intent.putExtra(CAMERA_TYPE, cameraId);
            intent.putExtra(CROP_MODE, cropMode);
            mContext.startActivityForResult(intent, idCardDirection);
        } else if (mFragment != null) {
            Intent intent = new Intent(mFragment.getActivity(), CameraActivity.class);
            intent.putExtra(TAKE_TYPE, idCardDirection);
            intent.putExtra(CAMERA_TYPE, cameraId);
            intent.putExtra(CROP_MODE, cropMode);
            mFragment.startActivityForResult(intent, idCardDirection);
        } else {
            return;
        }

    }

    /**
     * 获取图片路径
     *
     * @param data Intent
     * @return 图片路径
     */
    public static String getImagePath(Intent data) {
        if (data != null) {
            return data.getStringExtra(IMAGE_PATH);
        }
        return "";
    }


}

